import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:hitutor/Util/Global.dart';
import 'package:hitutor/Util/MyColor.dart';
import 'package:hitutor/Util/PageUtil.dart';
import 'package:hitutor/Widget/MyImage.dart';
import 'package:hitutor/Widget/MyPage.dart';
import 'package:webview_flutter/webview_flutter.dart';

class WebPage extends StatelessWidget {
  String title = "";
  String url = "";
  bool showBrowser;

  WebPage(this.title, this.url, {this.showBrowser = false});

  @override
  Widget build(BuildContext context) {
    return MyPage(
      title,
      Container(
          decoration: Global.getBg(MyColor.White, 0),
          child: WebView(
            initialUrl: url,
            javascriptMode: JavascriptMode.unrestricted,
              navigationDelegate: (NavigationRequest request) {
                if (request.url.startsWith('https://www.baidu.com')) {
                  print('allow navigation to ${request.url}}');
                  return NavigationDecision.navigate;
                }
                print('block navigation to ${request.url}');
                PageUtil().launchURL(request.url);
                return NavigationDecision.prevent;
              }
          )),
      rightWidgets: [
        Visibility(
            visible: showBrowser,
            child: LocalImage("browser.png", width: 44, height: 44, padding: EdgeInsets.all(10), onTap: () {
              PageUtil().launchURL(url);
            }))
      ],
    );
  }
}
